package io.renren.modules.secretary.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.common.validator.Assert;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.secretary.service.StudentService;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.form.PasswordForm;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static io.renren.common.utils.ShiroUtils.getUserId;

/**
 * @author wks
 * @date 2022/2/7 16:21
 */
@RestController
@RequestMapping("/secretary/student")
public class StudentController {

    @Autowired
    StudentService studentService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 所有学生列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        Page<Map<String, Object>> page = new Page<>( Long.valueOf(String.valueOf(params.get("page"))),  Long.valueOf(String.valueOf(params.get("limit"))));
        IPage<Map<String, Object>> mapIPage = studentService.selectStudentList(page, params);
        PageUtils newPage = new PageUtils(mapIPage);

        return R.ok().put("page", newPage);
    }

    /**
     * 学生角色列表
     */
    @GetMapping("/select")
    public R select() {
        Map<String, Object> map = new HashMap<>();
        map.put("role_id", 1);
        List<SysRoleEntity> list = (List<SysRoleEntity>) sysRoleService.listByMap(map);

        return R.ok().put("list", list);
    }


    /**
     * 保存用户
     */
    @SysLog("书记添加学生")
    @PostMapping("/save")
    public R save(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, AddGroup.class);
        user.setCreateUserId(ShiroUtils.getUserId());
        sysUserService.masterSaveTeacher(user);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("书记修改学生")
    @PostMapping("/update")
    //@RequiresPermissions("sys:user:update")
    public R update(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, UpdateGroup.class);
        user.setCreateUserId(ShiroUtils.getUserId());
        sysUserService.masterUpdateStudent(user);

        return R.ok();
    }
//
//    /**
//     * 删除用户
//     */
//    @SysLog("删除用户")
//    @PostMapping("/delete")
//    @RequiresPermissions("sys:user:delete")
//    public R delete(@RequestBody Long[] userIds){
//        if(ArrayUtils.contains(userIds, 1L)){
//            return R.error("系统管理员不能删除");
//        }
//
//        if(ArrayUtils.contains(userIds, getUserId())){
//            return R.error("当前用户不能删除");
//        }
//
//        sysUserService.deleteBatch(userIds);
//
//        return R.ok();
//    }

}
